<!DOCTYPE html>
<html class="x-admin-sm">
    
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../../../static/css/font.css">
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <link rel="stylesheet" href="../../../static/css/xadmin.css">
        <script type="text/javascript" src="../../../static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../../static/js/xadmin.js"></script>
        <script src="../../../static/js/axios.min.js" charset="utf-8"></script>
        <script src="../../../js/api/agency.js" charset="utf-8"></script>
        <script src="../../../js/api/group.js" charset="utf-8"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
                  <div class="layui-form-item">
                      <label for="groupName" class="layui-form-label">
                          <span class="x-red">*</span>部门名称
                      </label>
                      <div class="layui-input-inline">
                          <input type="text" id="groupName" name="groupName" required="" lay-verify="required"
                          autocomplete="off" class="layui-input">
                      </div>
                  </div>
                    <div class="layui-form-item ">
                        <label for="agencyCode" class="layui-form-label">
                            <span class="x-red">*</span>所属机构</label>
                        <div class="layui-input-inline" id="agencyCode" lay-filter="agencyCode">
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label for="parGroup" class="layui-form-label">
                            上级部门</label>
                        <div class="layui-input-inline">
                            <select name="parGroup" id="parGroup">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>

                  <div class="layui-form-item">
                      <label for="groupName" class="layui-form-label">
                      </label>
                      <button  class="layui-btn" lay-filter="add" lay-submit="">
                          保存
                      </button>
                  </div>
              </form>
            </div>
        </div>
        <script>
            layui.config({
                base: './../../../static/lib/dist/'
            })

            layui.use(['form', 'layer', 'selectPlus', 'notice'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                selectPlus = layui.selectPlus,
                notice = layui.notice,
                layer = layui.layer;

                var agencyinfo = null

                getChildAgencys().then(value => {
                    if(value.code == 200){
                        agencyinfo = selectPlus.render({
                            el: '#agencyCode',
                            data: value.data,
                            type: "radio",
                            valueName: "agencyCode",
                            label: ["agencyName"],
                            values: layui.data('user').info.agencyCode
                        });
                    }else{
                        notice.error('查询用户机构信息失败')
                    }
                })
                function setpargroup(agencyCode){
                    searchGroupOfAgency(agencyCode).then(value => {
                        if(value.code == 200){
                            $('#parGroup').empty()
                            $('#parGroup').append('<option value=""></option>');
                            if(value.data!=null && value.data.length>0){
                                value.data.forEach(vl => {
                                    let option = '<option value="'+vl.groupId+'">'+vl.groupName+'</option>';
                                    $('#parGroup').append(option);
                                })
                            }
                            form.render('select'); //刷新select选择框渲染
                        }else{
                            notice.error('查询部门信息失败')
                            $('#parGroup').empty()
                            $('#parGroup').append('<option value=""></option>');
                            form.render('select'); //刷新select选择框渲染
                        }
                    })
                }
                setpargroup(layui.data('user').info.agencyCode)
                selectPlus.on('selectPlus(agencyCode)', function (obj) {
                    setpargroup(obj.value)
                })


                //监听提交
                form.on('submit(add)',
                function(data) {
                    var obj = agencyinfo.getChecked();
                    let params = data.field
                    params.agencyCode = obj.values[0]
                    delete params.selectPlusradio1;
                    saveGroup(params).then(value => {
                        if(value.code == 200){
                            //发异步，把数据提交给php
                            layer.alert("保存成功", {
                                    icon: 6
                                },
                                function() {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        }else{
                            notice.error('保存部门信息失败，'+value.msg)
                        }
                    }).catch(reason => {
                        notice.error('服务器未连接')
                    })

                    return false;
                });

            });</script>
        <script>var _hmt = _hmt || []; (function() {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();</script>
    </body>

</html>
